package com.nike.amoeba.ui.frg

import android.os.Bundle
import android.widget.CompoundButton
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.LogUtils
import com.nike.amoeba.R
import com.nike.amoeba.api.UIHelper
import com.nike.amoeba.base.BaseFragment
import com.nike.amoeba.databinding.FLoginBinding
import com.nike.amoeba.ext.showMessage
import com.nike.amoeba.ui.act.LoginAct
import com.nike.amoeba.util.CacheUtil
import com.nike.amoeba.util.MusicUtils
import com.nike.amoeba.viewmodel.LoginModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import me.hgj.jetpackmvvm.ext.nav

/**
 * @Author nike
 * @Date 2023/7/5 14:53
 * @Description
 */
class LoginFrg: BaseFragment<LoginModel, FLoginBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewmodel = mViewModel
        mDatabind.click = ProxyClick()

        val user = CacheUtil.getUser()
        if (user != null){
            if (user.isRememberPwd){
                mViewModel.username.set(user.LoginID)
                mViewModel.password.set(user.Password)
            }
            mDatabind.cbPwd.isChecked = user.isRememberPwd
            mViewModel.isRememberPwd.set(mDatabind.cbPwd.isChecked)
        }
        mDatabind.tvVersion.text = "Copyright © 2025 By SPIT ."
    }

    override fun createObserver() {

    }

    inner class ProxyClick() {
        fun clear(){
            mViewModel.username.set("")
        }

        var onCheckedChangeListener =
            CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
                mViewModel.isShowPwd.set(isChecked)
            }

        var onCheckedChangeRememberPwdListener =
            CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
                mViewModel.isRememberPwd.set(isChecked)
            }

        fun login(){
            when {
                mViewModel.username.get().isEmpty() -> showMessage(getString(R.string.error_phone))
                mViewModel.password.get().isEmpty() -> showMessage(getString(R.string.error_phone))
                else -> mViewModel.login(
                    mViewModel.username.get(),
                    mViewModel.password.get()
                )
            }
        }

        fun toSet(){
            UIHelper.startSettingFrg(nav())
        }

    }

}
